﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chefbook.DataObjects;
using System.Windows.Input;
using Chefbook.UserActions;

namespace Chefbook.ViewModels
{
    /// <summary>
    /// A UI-friendly wrapper for an Ingredient
    /// </summary>
    public class IngredientViewModel : ViewModelBase
    {
        /// <summary>
        /// Gets the Ingredient being wrapped
        /// </summary>
        public Ingredient IngredientModel { get; private set; }

        /// <summary>
        /// Creates a new UI-friendly wrapper for an Ingredient
        /// </summary>
        /// <param name="ingredient">The Ingredient to be wrapped</param>
        public IngredientViewModel(Ingredient ingredient)
        {
            this.IngredientModel = ingredient;
            ingredient.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(ingredient_PropertyChanged);
        }

        /// <summary>
        /// Raised when this Ingredient is requested to be removed
        /// </summary>
        public event EventHandler IngredientDeleted = (s, a) => { };

        ICommand removeIngredientCommand;
        /// <summary>
        /// Command for removing this Ingredient from it's recipe
        /// </summary>
        public ICommand RemoveIngredientCommand
        {
            get
            {
                if (removeIngredientCommand == null)
                {
                    removeIngredientCommand = new CustomCommand((param) => IngredientDeleted(this, new EventArgs()));
                }
                return removeIngredientCommand;
            }
        }

        /// <summary>
        /// Gets or sets the amount of the ingredient
        /// </summary>
        public float Amount
        {
            get { return IngredientModel.Amount; }
            set { IngredientModel.Amount = value; }
        }

        /// <summary>
        /// Gets or sets the name of the ingredient
        /// </summary>
        public string Name
        {
            get { return IngredientModel.Name; }
            set { IngredientModel.Name = value; }
        }

        /// <summary>
        /// Gets or sets the units of the ingredient
        /// </summary>
        public string Units
        {
            get { return IngredientModel.Units; }
            set { IngredientModel.Units = value; }
        }
        
        /// <summary>
        /// Compares whether this object wraps the same Ingredient as the parameter
        /// </summary>
        /// <param name="obj">The object to compare equality with</param>
        /// <returns>True if they wrap the same Ingredient</returns>
        public override bool Equals(object obj)
        {
            var comparedIngredient = obj as IngredientViewModel;
            if (comparedIngredient == null)
            { return false; }
            return IngredientModel == comparedIngredient.IngredientModel;
        }

        /// <summary>
        /// Gets the hash code of the wrapped ingredient
        /// </summary>
        /// <returns>The hashcode</returns>
        public override int GetHashCode()
        {
            return IngredientModel.GetHashCode();
        }

        void ingredient_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "Amount":
                    OnPropertyChanged("Amount");
                    break;
                case "Name":
                    OnPropertyChanged("Name");
                    break;
                case "Units":
                    OnPropertyChanged("Units");
                    break;
                case "Recipe":
                    //Do nothing
                    break;
                default:
                    throw new NotImplementedException(e.PropertyName);
            }
        }
    }
}